<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreHelper;
use App\Http\Requests\UpdateHelper;
use App\Models\Helper;
use foo\bar;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HelperController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    public function __construct()
    {
        view()->share([
            '_ygs' => 'am-in',
            '_helper' => 'am-active',
        ]);

    }
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            //按助理名字名称
            if($request->has('name')and $request->name !=''){
                $search = "%".$request->name."%";
                $query->where('name','like',$search);
            }
        };

        $helpers=Helper::where($where)->paginate(15);
       return view('admin.helper.index',compact('helpers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return view('admin.helper.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreHelper $request)
    {
//        return $request->header('accept-language');
        Helper::create($request->all());
        return redirect('/admin/helper')->with('success','新增成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $helper=Helper::find($id);
        return view('admin.helper.edit',compact('helper'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateHelper $request, $id)
    {
        $helper=Helper::find($id);
        $data['name']=$request->name;
        $info=Helper::where($data)->first();
        $item=Helper::where('number',$request->number)->first();
        if($helper['name']!=$request->name && isset($info)){
            return back()->with('success','所修改姓名已经有人使用');
        }
        if($helper['number']!=$request->number && isset($item)){
            return back()->with('success','所修改编号已经有人使用');
        }
        else{
            $helper->update($request->all());
            return redirect('/admin/helper')->with('success','修改成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
      Helper::destroy($id);
      return back()->with('success','删除成功');
    }

    public function delete_all(Request $request){


        Helper::destroy($request->checked_id);
    }
}
